﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Xml;

namespace ESOL.AutoUpdate
{
    public partial class frmHostSetting : Form
    {
        public frmHostSetting()
        {
            InitializeComponent();
        }

        private void frmHostSetting_Load(object sender, EventArgs e)
        {
        }
        private void UpdateConfig(string Xvalue)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Application.ExecutablePath + ".config ");
            XmlNode node = doc.SelectSingleNode(@"//add[@key= 'ServerName '] ");
            XmlElement ele = (XmlElement)node;
            ele.SetAttribute("value ", Xvalue);
            doc.Save(Application.ExecutablePath + ".config ");
        }
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtHostAddress.Text))
            {
                MessageBox.Show("请输入一个主机IP地址");
                return;
            }
            string HostAdress = txtHostAddress.Text.Trim();
            try 
            {

                XmlDocument doc = new XmlDocument();
                doc.Load(Application.ExecutablePath + ".config ");
                (doc.SelectSingleNode(@"configuration").SelectSingleNode(@"applicationSettings").SelectSingleNode(@"ESOL.Update.Properties.Settings").SelectSingleNode(@"setting").SelectSingleNode(@"value")).InnerText = HostAdress;
                doc.Save(Application.ExecutablePath + ".config ");

                //Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                //AppSettingsSection appSettings = config.AppSettings;
                //appSettings.Settings["ESOL.Update.Properties.Settings"].Value = HostAdress;
                //config.Save(ConfigurationSaveMode.Modified);
                this.DialogResult = DialogResult.OK;
                this.Dispose();
            }
            catch(Exception ex) 
            {
                MessageBox.Show("设置失败："+ex.Message);
                return;
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Dispose();
        }
    }
}
